package com.hermit.qmst.tools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.widget.ImageView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2017/1/2 0002.
 */
public class RequestHttp {
    private static String TAG = "RequestHttp";
    private static ProgressDialog mProgressDialog;
    private static String tips = "系统正在处理，请稍后......";

    public interface HttpCallBack {
        void successHttp(JSONObject jsonObject);
    }

    public static void getHttp(final Activity ac, String url, final HttpCallBack httpCallBack) {
        mProgressDialog = ProgressDialog.show(ac, "提示", tips, false, false);
        android.util.Log.i(TAG, url);
//        x.http().
        RequestParams params = new RequestParams(url);
        params.setConnectTimeout(5000);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                        android.util.Log.i(TAG, result);
                        httpCallBack.successHttp(new JSONObject(result));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    android.util.Log.e(TAG, "JSONException: " + e.getMessage());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
                try {
                    android.util.Log.e(TAG, ex.getMessage());
                    Toast.makeText(ac, ex.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
                android.util.Log.v(TAG, "getHttp请求取消" + cex.getMessage());
            }

            @Override
            public void onFinished() {
                android.util.Log.v(TAG, "getHttp请求完成");
            }
        });
    }

    public static void getHttpNoDialog(final Activity ac, String url, final HttpCallBack httpCallBack) {
        android.util.Log.i(TAG, url);
        RequestParams params = new RequestParams(url);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                android.util.Log.i(TAG, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                    android.util.Log.e(TAG, ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                android.util.Log.v(TAG, "getHttp请求取消" + cex.getMessage());
            }

            @Override
            public void onFinished() {
                android.util.Log.v(TAG, "getHttp请求完成");
            }
        });
    }

    public static void postHttp(final Activity ac, RequestParams params, final HttpCallBack httpCallBack) {
        mProgressDialog = ProgressDialog.show(ac, "提示", tips, false, false);
        android.util.Log.i(TAG, params.getUri());
        android.util.Log.i(TAG, params.getStringParams().toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();

                        android.util.Log.i(TAG, result);
                        httpCallBack.successHttp(new JSONObject(result));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    android.util.Log.e(TAG, "JSONException: " + e.getMessage());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
                android.util.Log.e(TAG, "getHttp请求失败" + ex.getMessage());
                Toast.makeText(ac, "", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                android.util.Log.v(TAG, "getHttp请求取消" + cex.getMessage());
            }

            @Override
            public void onFinished() {
                android.util.Log.v(TAG, "getHttp请求完成");
            }
        });
    }

    public static void downToIamge(Activity ac, String url, ImageView iv) {
        android.util.Log.i(TAG, "downToIamge:" + url);
        x.image().bind(iv, url);
    }


    public static void uploadImage(Activity mAct, RequestParams requestParams, final HttpCallBack httpCallBack) {
        android.util.Log.i(TAG, requestParams.toString());
        android.util.Log.i(TAG, requestParams.getStringParams().toString());
        mProgressDialog = ProgressDialog.show(mAct, "提示", "图片正在上传中，请稍后...", false, false);
        x.http().post(requestParams, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                if (mProgressDialog != null) {
                    android.util.Log.i(TAG, jsonObject.toString());
                    httpCallBack.successHttp(jsonObject);
                    mProgressDialog.dismiss();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                android.util.Log.i(TAG, ex.getMessage());
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
                if (ex instanceof HttpException) { // 网络错误
                    HttpException httpEx = (HttpException) ex;
                    int responseCode = httpEx.getCode();
                    String responseMsg = httpEx.getMessage();
                    String errorResult = httpEx.getResult();
                } else {
                }
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
